//
//  NewsDetailViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "NewsDetailTableViewCell.h"

@interface NewsDetailViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation NewsDetailViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = KBACKGROUNDCOLOR;
    self.title = self.titleStr;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewsDetailTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"NewsDetailTableViewCell"];
    [self creatDataList];
    
    if (self.scanNewsDetailBlock) {
        self.scanNewsDetailBlock();
    }
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.newsId forKey:@"id"]; //1：查询朋友互动类型 2
    [dic setValue:self.newsType forKey:@"type"]; //1：查询朋友互动类型 2进入系统通知详情   3进入校园圈消息点赞
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppMessageGetNoticeDetailById] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];
            [NewsInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                return @{@"newsId" : @"id"};
            }];
            [self.dataArray addObjectsFromArray: [NewsInfo mj_objectArrayWithKeyValuesArray:response[@"list"]]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsDetailTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"NewsDetailTableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = KBACKGROUNDCOLOR;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.newsInfo = self.dataArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsInfo *newsInfo = self.dataArray[indexPath.section];
    MyLog(@"%f",[Global getSizeOfString:newsInfo.show_text maxWidth:kScreenW - 10 maxHeight:CGFLOAT_MAX withFontSize:14.6].height) ;
    return [Global getSizeOfString:newsInfo.context maxWidth:kScreenW - 10 maxHeight:CGFLOAT_MAX withFontSize:14.6].height + 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 40);
//    view.backgroundColor = KBACKGROUNDCOLOR;
    
    NewsInfo *newsInfo = self.dataArray[section];
    double timeWidth = [Global getSizeOfString:newsInfo.add_time maxWidth:kScreenW maxHeight:20 withFontSize:14].width + 10;
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.frame = CGRectMake((kScreenW - timeWidth)/2, 10, timeWidth, 20);
    timeLabel.backgroundColor = RGBColor(219, 219, 219);
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = [UIFont systemFontOfSize:14];
    timeLabel.text = newsInfo.add_time;
    timeLabel.clipsToBounds = YES;
    timeLabel.layer.cornerRadius = 2;
    [view addSubview:timeLabel];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
